{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DC Line dispatch with pandapower OPF\n",
    "This is an introduction into the usage of the pandapower optimal power flow with dc lines.\n",
    "\n",
    "## Example Network\n",
    "\n",
    "We use the following four bus example network for this tutorial:\n",
    "\n",
    "<img src=\"pics/example_opf_dcline.png\" width=\"100%\">\n",
    "\n",
    "We first create this network in pandapower:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandapower as pp\n",
    "from numpy import array\n",
    "net = pp.create_empty_network()\n",
    "\n",
    "b1 = pp.create_bus(net, 380)\n",
    "b2 = pp.create_bus(net, 380)\n",
    "b3 = pp.create_bus(net, 380)\n",
    "b4 = pp.create_bus(net, 380)\n",
    "b5 = pp.create_bus(net, 380)\n",
    "\n",
    "l1 = pp.create_line(net, b1, b2, 30, \"490-AL1/64-ST1A 380.0\")\n",
    "l2 = pp.create_line(net, b3, b4, 20, \"490-AL1/64-ST1A 380.0\")\n",
    "l3 = pp.create_line(net, b4, b5, 20, \"490-AL1/64-ST1A 380.0\")\n",
    "\n",
    "dcl1 = pp.create_dcline(net, name=\"dc line\", from_bus=b2, to_bus=b3, p_mw=200, loss_percent=1.0, \n",
    "                  loss_mw=0.5, vm_from_pu=1.01, vm_to_pu=1.012, max_p_mw=1000,\n",
    "                  in_service=True)\n",
    "\n",
    "eg1 = pp.create_ext_grid(net, b1, 1.02, min_p_mw=0.)\n",
    "eg2 = pp.create_ext_grid(net, b5, 1.02, min_p_mw=0.)\n",
    "\n",
    "l1 = pp.create_load(net, bus=b4, p_mw=800, controllable = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We now run a regular load flow to check out the DC line model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp.runpp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The transmission power of the DC line is defined in the loadflow as given by the p_kw parameter, which was set to 200 MW:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>200.0</td>\n",
       "      <td>152.443185</td>\n",
       "      <td>-197.5</td>\n",
       "      <td>74.491759</td>\n",
       "      <td>2.5</td>\n",
       "      <td>1.01</td>\n",
       "      <td>-0.48595</td>\n",
       "      <td>1.012</td>\n",
       "      <td>-0.725627</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   p_from_mw  q_from_mvar  p_to_mw  q_to_mvar  pl_mw  vm_from_pu  \\\n",
       "0      200.0   152.443185   -197.5  74.491759    2.5        1.01   \n",
       "\n",
       "   va_from_degree  vm_to_pu  va_to_degree  \n",
       "0        -0.48595     1.012     -0.725627  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The losses amount to 2.5 MW, which are made up of 0.5 MW conversion loss and 200 MW * 0.01 = 2 MW transmission losses. The voltage setpoints defined at from and to bus are complied with. \n",
    "\n",
    "Now lets define costs for the external grids to run an OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "costeg0 = pp.create_poly_cost(net, 0, 'ext_grid', cp1_eur_per_mw=10)\n",
    "costeg1 = pp.create_poly_cost(net, 1, 'ext_grid', cp1_eur_per_mw=8)\n",
    "net.bus['max_vm_pu'] = 1.5\n",
    "net.line['max_loading_percent'] = 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['dcline']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n"
     ]
    }
   ],
   "source": [
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since we defined lower costs for Ext Grid 2, it fully services the load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.500070</td>\n",
       "      <td>-7.787527</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>805.091485</td>\n",
       "      <td>0.628364</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p_mw    q_mvar\n",
       "0    0.500070 -7.787527\n",
       "1  805.091485  0.628364"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While the DC line does not transmit any power:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.500067</td>\n",
       "      <td>7.787487</td>\n",
       "      <td>-0.000066</td>\n",
       "      <td>-0.62707</td>\n",
       "      <td>0.500001</td>\n",
       "      <td>1.019994</td>\n",
       "      <td>-0.001448</td>\n",
       "      <td>1.013925</td>\n",
       "      <td>-1.563437</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   p_from_mw  q_from_mvar   p_to_mw  q_to_mvar     pl_mw  vm_from_pu  \\\n",
       "0   0.500067     7.787487 -0.000066   -0.62707  0.500001    1.019994   \n",
       "\n",
       "   va_from_degree  vm_to_pu  va_to_degree  \n",
       "0       -0.001448  1.013925     -1.563437  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we set the costs of the left grid to a lower value than the right grid and run the loadflow again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['dcline']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n"
     ]
    }
   ],
   "source": [
    "net.poly_cost.cp1_eur_per_mw.at[costeg0] = 8\n",
    "net.poly_cost.cp1_eur_per_mw.at[costeg1] = 10\n",
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the power now comes from the left ext_grid:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>821.524623</td>\n",
       "      <td>-7.787199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000759</td>\n",
       "      <td>-21.048015</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p_mw     q_mvar\n",
       "0  821.524623  -7.787199\n",
       "1    0.000759 -21.048015"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And is transmitted over the DC line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>813.573163</td>\n",
       "      <td>-26.445627</td>\n",
       "      <td>-805.022934</td>\n",
       "      <td>-21.735698</td>\n",
       "      <td>8.550229</td>\n",
       "      <td>1.011014</td>\n",
       "      <td>-2.399868</td>\n",
       "      <td>1.027504</td>\n",
       "      <td>1.522331</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    p_from_mw  q_from_mvar     p_to_mw  q_to_mvar     pl_mw  vm_from_pu  \\\n",
       "0  813.573163   -26.445627 -805.022934 -21.735698  8.550229    1.011014   \n",
       "\n",
       "   va_from_degree  vm_to_pu  va_to_degree  \n",
       "0       -2.399868  1.027504      1.522331  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can however see that the lines on the left hand side are now overloaded:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    127.474837\n",
       "1    124.074276\n",
       "2      3.265845\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we set the maximum line loading to 100% and run the OPF again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['dcline']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n"
     ]
    }
   ],
   "source": [
    "net.line[\"max_loading_percent\"] = 100\n",
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the lines are no longer overloaded:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    100.000007\n",
       "1     97.795377\n",
       "2     26.591814\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because the load is serviced from both grids:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>644.488864</td>\n",
       "      <td>-0.680455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>170.582480</td>\n",
       "      <td>-16.528459</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p_mw     q_mvar\n",
       "0  644.488864  -0.680455\n",
       "1  170.582480 -16.528459"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the DC line transmits only part of the power needed to service the load:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>639.594579</td>\n",
       "      <td>-6.207845</td>\n",
       "      <td>-632.76691</td>\n",
       "      <td>-10.099065</td>\n",
       "      <td>6.827669</td>\n",
       "      <td>1.012431</td>\n",
       "      <td>-1.875022</td>\n",
       "      <td>1.024385</td>\n",
       "      <td>0.875621</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    p_from_mw  q_from_mvar    p_to_mw  q_to_mvar     pl_mw  vm_from_pu  \\\n",
       "0  639.594579    -6.207845 -632.76691 -10.099065  6.827669    1.012431   \n",
       "\n",
       "   va_from_degree  vm_to_pu  va_to_degree  \n",
       "0       -1.875022  1.024385      0.875621  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can also define transmission costs for the DC line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['dcline']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n"
     ]
    }
   ],
   "source": [
    "costeg1 = pp.create_poly_cost(net, 0, 'dcline', cp1_eur_per_mw=3)\n",
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because the sum of the costs for generating power on the left hand side (8) and transmitting it to the right side (3) is now larger than for generating on the right side (10), the OPF draws as much power from the right side as is possible without violating line loading constraints:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     25.067630\n",
       "1     24.770581\n",
       "2    100.000029\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>161.063564</td>\n",
       "      <td>6.442959</td>\n",
       "      <td>-158.973825</td>\n",
       "      <td>4.897226</td>\n",
       "      <td>2.089738</td>\n",
       "      <td>1.018095</td>\n",
       "      <td>-0.467991</td>\n",
       "      <td>1.016202</td>\n",
       "      <td>-0.938724</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    p_from_mw  q_from_mvar     p_to_mw  q_to_mvar     pl_mw  vm_from_pu  \\\n",
       "0  161.063564     6.442959 -158.973825   4.897226  2.089738    1.018095   \n",
       "\n",
       "   va_from_degree  vm_to_pu  va_to_degree  \n",
       "0       -0.467991  1.016202     -0.938724  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we relax the line loading constraint and run the OPF again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['dcline']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n"
     ]
    }
   ],
   "source": [
    "net.line[\"max_loading_percent\"] = 1000\n",
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The load is once again fully serviced by the grid on the right hand side:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.503447</td>\n",
       "      <td>-7.787432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>805.088098</td>\n",
       "      <td>0.628941</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p_mw    q_mvar\n",
       "0    0.503447 -7.787432\n",
       "1  805.088098  0.628941"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the DC line is in open loop operation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.503444</td>\n",
       "      <td>7.787581</td>\n",
       "      <td>-0.00341</td>\n",
       "      <td>-0.626309</td>\n",
       "      <td>0.500034</td>\n",
       "      <td>1.019994</td>\n",
       "      <td>-0.001457</td>\n",
       "      <td>1.013925</td>\n",
       "      <td>-1.563423</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   p_from_mw  q_from_mvar  p_to_mw  q_to_mvar     pl_mw  vm_from_pu  \\\n",
       "0   0.503444     7.787581 -0.00341  -0.626309  0.500034    1.019994   \n",
       "\n",
       "   va_from_degree  vm_to_pu  va_to_degree  \n",
       "0       -0.001457  1.013925     -1.563423  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_dcline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Little consistency check:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8056.418890966571"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid.p_mw.at[0]*8 + net.res_ext_grid.p_mw.at[1]*10 + net.res_dcline.p_from_mw.at[0]*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8056.418890966571"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
